package com.zbkj.front.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.zbkj.common.model.standard.PeStandard;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.standard.QueryStandByTypeRequest;
import com.zbkj.common.response.standard.StandInfoResponse;
import com.zbkj.common.response.standard.StandardListResponse;
import com.zbkj.front.service.PeStandardHandleService;
import com.zbkj.service.service.standard.IPeStandardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Service
public class PeStandardHandleServiceImpl implements PeStandardHandleService {

    @Autowired
    private IPeStandardService peStandardService;

    @Override
    public PageInfo<StandardListResponse> standList(QueryStandByTypeRequest pageParamRequest) {
        PageInfo<PeStandard> page = peStandardService.standList(pageParamRequest);
        List<StandardListResponse> responseList = Lists.newArrayList();
        for(PeStandard peStandard : page.getList()) {
            StandardListResponse response = new StandardListResponse();
            BeanUtils.copyProperties(peStandard, response);
            responseList.add(response);
        }
        return CommonPage.copyPageInfo(page, responseList);
    }

    @Override
    public List<StandInfoResponse> standInfo(Long standId) {
        List<PeStandard> list = peStandardService.lambdaQuery().eq(PeStandard::getRootId, standId).eq(PeStandard::getDeleteFlag, 0).list();
        List<StandInfoResponse> dbList = new ArrayList<>();
        for (PeStandard peStandard : list) {
            StandInfoResponse standInfoResponse = new StandInfoResponse();
            BeanUtils.copyProperties(peStandard, standInfoResponse);
            dbList.add(standInfoResponse);
        }

        Map<Long, List<StandInfoResponse>> childMap = new HashMap<>();
        dbList.forEach(item -> {
            if(childMap.containsKey(item.getParentId())){
                childMap.get(item.getParentId()).add(item);
            }else{
                ArrayList<StandInfoResponse> peStandards = new ArrayList<>();
                peStandards.add(item);
                childMap.put(item.getParentId(), peStandards);
            }
        });

        List<StandInfoResponse> levelFour = dbList.stream().filter(item -> item.getLevel() == 4).collect(Collectors.toList());
        for (StandInfoResponse peStandard : levelFour) {
            List<StandInfoResponse> peStandards = childMap.get(peStandard.getId());
            if(ObjectUtils.isNotEmpty(peStandards)){
                peStandard.setChilds(peStandards);
            }
        }

        List<StandInfoResponse> levelThree = dbList.stream().filter(item -> item.getLevel() == 3).collect(Collectors.toList());
        for (StandInfoResponse peStandard : levelThree) {
            List<StandInfoResponse> peStandards = childMap.get(peStandard.getId());
            if(ObjectUtils.isNotEmpty(peStandards)){
                peStandard.setChilds(peStandards);
            }
        }

        List<StandInfoResponse> levelTwo = dbList.stream().filter(item -> item.getLevel() == 2).collect(Collectors.toList());
        for (StandInfoResponse peStandard : levelTwo) {
            List<StandInfoResponse> peStandards = childMap.get(peStandard.getId());
            if(ObjectUtils.isNotEmpty(peStandards)){
                peStandard.setChilds(peStandards);
            }
        }

        List<StandInfoResponse> levelOne = dbList.stream().filter(item -> item.getLevel() == 1).collect(Collectors.toList());
        for (StandInfoResponse peStandard : levelOne) {
            List<StandInfoResponse> peStandards = childMap.get(peStandard.getId());
            if(ObjectUtils.isNotEmpty(peStandards)){
                peStandard.setChilds(peStandards);
            }
        }
        return levelOne;
    }


}
